package com.ev.scm.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.ev.framework.config.ConstantForGYL;
import com.ev.framework.config.ConstantForMES;
import com.ev.mes.domain.MaterialInspectionDO;
import com.ev.scm.dao.QrcodeDao;
import com.ev.scm.domain.*;
import com.ev.scm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class QrcodeServiceImpl implements QrcodeService {
    @Autowired
    private QrcodeDao qrcodeDao;

    @Autowired
    private StockOutService stockOutService;

    @Autowired
    private StockService stockService;

    @Autowired
    private StockInItemService stockInItemService;

    @Autowired
    private QrcodeItemService qrcodeItemService;

    @Override
    public QrcodeDO get(Long id) {
        return qrcodeDao.get(id);
    }

    @Override
    public List<QrcodeDO> list(Map<String, Object> map) {
        return qrcodeDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return qrcodeDao.count(map);
    }

    @Override
    public List<Map<String, Object>> listForMap(Map<String, Object> map) {
        return qrcodeDao.listForMap(map);
    }

    @Override
    public int countForMap(Map<String, Object> map) {
        return qrcodeDao.countForMap(map);
    }

    @Override
    public int save(QrcodeDO qrcode) {
        return qrcodeDao.save(qrcode);
    }

    @Override
    public int batchInsert(List<QrcodeDO> qrcodeDOList) {
        return qrcodeDao.batchInsert(qrcodeDOList);
    }

    @Override
    public int update(QrcodeDO qrcode) {
        return qrcodeDao.update(qrcode);
    }

    @Override
    public int batchUpdate(List<QrcodeDO> qrcodeDOList) {
        return qrcodeDao.batchUpdate(qrcodeDOList);
    }

    @Override
    public int remove(Long id) {
        return qrcodeDao.remove(id);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return qrcodeDao.batchRemove(ids);
    }

    @Override
    public Boolean isMultipleType(Integer inType, MaterialInspectionDO materialInspectionDO) {
        if(inType==0){
            if(!Objects.equals(materialInspectionDO.getInspectionType(), ConstantForMES.LLJY)){
                return false;
            }else if(!StringUtils.isEmpty(materialInspectionDO.getSourceNo()) && !Objects.equals(Long.parseLong(materialInspectionDO.getSourceType().toString()), ConstantForGYL.CGHT)){
                return false;
            }
            //TODO 对于没有源单的来料检验是否要区分（暂无区分办法）
        }else if(inType==1){
            if(!Objects.equals(materialInspectionDO.getInspectionType(),ConstantForMES.CPJY)){
                return false;
            }
        }else if(inType==2){
            if(!Objects.equals(materialInspectionDO.getInspectionType(),ConstantForMES.LLJY)){
                return false;
            }else if(!StringUtils.isEmpty(materialInspectionDO.getSourceNo()) && !Objects.equals(Long.parseLong(materialInspectionDO.getSourceType().toString()), ConstantForGYL.WWHT)){
                return false;
            }
            //TODO 对于没有源单的来料检验是否要区分（暂无区分办法）
        }else if(inType==3){
            //TODO
        }
        return true;
    }

    /**
     * 判断是否多单入库
     * @param isMultiple 1：是，0：否
     * @param contractNo 合同编号
     * @param materialInspectionDO 检验单
     * @return 返回结果
     */
    @Override
    public Boolean isMultipleIn(Integer isMultiple, String contractNo, MaterialInspectionDO materialInspectionDO) {
        if(isMultiple==0 && !StringUtils.isEmpty(contractNo) && !Objects.equals(contractNo,materialInspectionDO.getSourceNo())){
            return false;
        }
        return true;
    }

    /**
	 * 入库后调用方法保存二维码信息
     * @param stockInDO 入库主表信息
	 * @param stockDOS 保存的库存列表
	 * @param params 前端扫码参数列表[{......,qrCodeId:1}{......,qrCodeId:2}]
	 */
    @Override
    public void saveInQrCode(StockInDO stockInDO, List<StockDO> stockDOS, List<StockInItemDO> params) {
        List<StockInItemDO> stockInItemDOS = stockInItemService.list(new HashMap<String,Object>(){{put("inheadId",stockInDO.getId());}});
        List<QrcodeDO> qrcodeDOList = new ArrayList<>();
        List<QrcodeItemDO> qrcodeItemDOList = new ArrayList<>();
        /**
         * 整理数量，批量更新
         */
        for (StockInItemDO stockInItemDO : params) {
            for (StockDO stockDO : stockDOS) {
                /**
                 * 库位相同，物料相同，批号相同的分组
                 */
                if (Objects.equals(stockDO.getMaterielId(), stockInItemDO.getMaterielId()) &&
                        (stockDO.getWarehouse() + "-" + stockDO.getWarehLocation()).equals(stockInItemDO.getWarehouse() + "-" + stockInItemDO.getWarehLocation()) &&
                        stockInItemDO.getBatch()==null?true:stockInItemDO.getBatch().equals(stockDO.getBatch())) {
                    QrcodeDO qrcodeDO = qrcodeDao.get(stockInItemDO.getQrcodeId());
                    //主要应对整包装出库以后退货的场景，所以每次入库的时候都将剩余数量重置为二维码的数量，不支持拆包装出库再退货的场景。
                    qrcodeDO.setRemainCount(qrcodeDO.getCount());
                    qrcodeDO.setStockId(stockDO.getId());
                    qrcodeDOList.add(qrcodeDO);
                    break;
                }
            }
            for (StockInItemDO stockInItemDOSaved : stockInItemDOS) {
                /**
                 * 库位相同，物料相同，批号相同的分组
                 */
                if (Objects.equals(stockInItemDOSaved.getMaterielId(), stockInItemDO.getMaterielId()) &&
                        (stockInItemDOSaved.getWarehouse() + "-" + stockInItemDOSaved.getWarehLocation()).equals(stockInItemDO.getWarehouse() + "-" + stockInItemDO.getWarehLocation()) &&
                        stockInItemDO.getBatch()==null?true:stockInItemDO.getBatch().equals(stockInItemDOSaved.getBatch())) {
                    QrcodeItemDO qrcodeItemDO = new QrcodeItemDO(stockInItemDO.getQrcodeId(), stockInDO.getStorageType(), stockInDO.getId(),stockInDO.getInheadCode(), stockInItemDOSaved.getId(), stockInItemDO.getCount());
                    qrcodeItemDO.setOperateType(0);
                    qrcodeItemDOList.add(qrcodeItemDO);
                    break;
                }
            }
        }
        batchUpdate(qrcodeDOList);
        qrcodeItemService.batchInsert(qrcodeItemDOList);
    }

	/**
	 *
	 * @param params 前端扫码参数列表[{......,qrCodeId:1,count:5}{......,qrCodeId:2,count:5}]
	 */
	@Override
    public void saveOutQrCode(List<StockOutItemDO> stockOutItems, List<StockOutItemDO> params) {
        List<QrcodeDO> qrcodeDOList = new ArrayList<>();
        List<QrcodeItemDO> qrcodeItemDOList = new ArrayList<>();
        /**
         * 获取出库类型
         */
        StockOutDO stockOutDO = stockOutService.get(stockOutItems.get(0).getOutId());
		for(StockOutItemDO stockOutItemDO : params){
		    Long qrCodeId = stockOutItemDO.getQrcodeId();
            QrcodeDO qrcodeDO = qrcodeDao.get(qrCodeId);
			for(StockOutItemDO stockOutItemDOSaved : stockOutItems){
                if(Arrays.asList(stockOutItemDOSaved.getStockId().split(",")).contains(qrcodeDO.getStockId().toString())){
                    qrcodeDO.setRemainCount(qrcodeDO.getRemainCount().subtract(stockOutItemDO.getCount()));
                    qrcodeDOList.add(qrcodeDO);
                    QrcodeItemDO qrcodeItemDO = new QrcodeItemDO(qrCodeId, stockOutDO.getOutboundType(), stockOutDO.getId(), stockOutDO.getOutCode(), stockOutItemDOSaved.getId(), stockOutItemDO.getCount());
                    qrcodeItemDO.setOperateType(1);
                    qrcodeItemDOList.add(qrcodeItemDO);
                    break;
                }
			}
		}
		batchUpdate(qrcodeDOList);
		qrcodeItemService.batchInsert(qrcodeItemDOList);
    }

    @Override
    public void transferHandler(List<StockDO> stockDOS, List<AllotItemDO> allotItemDOS) {
        //先将二维码的stockId修改为最新的
        List<QrcodeDO> qrcodeDOList = new ArrayList<>();
        for(AllotItemDO allotItemDO : allotItemDOS){
            QrcodeDO qrcodeDO = qrcodeDao.get(allotItemDO.getQrcodeId());
            StockDO stockDOTemp = stockService.get(Long.parseLong(allotItemDO.getStockId().split(",")[0]));
            for(StockDO stockDO : stockDOS){
                if(Objects.equals(stockDO.getMaterielId(), stockDOTemp.getMaterielId()) &&
                        (stockDO.getWarehouse() + "-" + stockDO.getWarehLocation()).equals(allotItemDO.getInFacility() + "-" + allotItemDO.getInLocation()) &&
                        stockDO.getBatch()==null?true:stockDO.getBatch().equals(stockDOTemp.getBatch())){
                    qrcodeDO.setStockId(stockDO.getId());
                    qrcodeDOList.add(qrcodeDO);
                    break;
                }
            }
        }
        batchUpdate(qrcodeDOList);
        //TODO 看需求是否需要保存二维码的变更历史（暂时没什么意义）

    }

    public static void main(String[] args) {
        System.out.println(Objects.equals(216L,216));
    }

}
